<template>
  <div class="app-container">
    <!-- 筛选区域 -->
    <div class="filter-container">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="文章类型：">
          <el-select 
            v-model="filterForm.categoryId"
            placeholder="请选择类型" 
            clearable
            style="width: 150px"
          >
            <el-option
              v-for="item in categoryOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        
        <el-form-item label="文章标题：">
          <el-input 
            v-model="filterForm.title" 
            placeholder="请输入标题" 
            clearable 
            style="width: 200px"
          />
        </el-form-item>
      
        <el-form-item label="发布时间：">
          <el-date-picker
            v-model="filterForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            style="width: 310px;"
          />
        </el-form-item>
        
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 数据表格 -->
    <el-table
      :data="tableData"
      border
      style="width: 100%; margin-top: 20px;"
    >
      <el-table-column prop="id" label="ID" width="80" align="center" />
      <el-table-column prop="title" label="标题" min-width="200" />
      <el-table-column prop="author" label="作者" width="120" align="center" />
      <el-table-column prop="summary" label="摘要" min-width="180" />
      <el-table-column prop="createDate" label="发表时间" width="160" />
      <el-table-column prop="modifyDate" label="修改时间" width="160" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.status == 1 ? 'success' : 'info'">
            {{ row.status == 1 ? '已发布' : '草稿' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template #default="{ row }">
          <el-link type="primary" @click="handleView(row)">查看</el-link>
          <el-divider direction="vertical" />
          <el-link type="danger" @click="handleDelete(row)">删除</el-link>
          <el-divider direction="vertical" />
          <el-link type="success" @click="handleEdit(row)">修改</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      class="pagination-container"
      v-model:current-page="pageNum"
      v-model:page-size="pageSize"
      :page-sizes="[3, 5, 10, 20]"
      :background="true"
      layout="total, sizes, prev, pager, next"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import axios from "axios";

export default {
  data(){
    return{
      // 筛选表单
      filterForm:{
        title: '',
        categoryId: '',
        dateRange: []
      },
      // 分类选项
     categoryOptions :null,
      tableData:null,
      // 分页配置
      pageNum: 1,
      pageSize: 5,
      total: null,

    }

  },
  methods:{
    // 新闻分类
    category(){
      axios.get("http://localhost:8080/cate/category"
      ).then((resp) => {
        this.categoryOptions = resp.data.data
        console.log(this.categoryOptions)
      })
    },
    // 编辑操作
    handleEdit(row){
      console.log('编辑：', row);
      this.$router.push(`/apinews/edit/${row.id}`);
    },
    showData(){
      axios.get("http://localhost:8080/news/list",
          {
            params:{
              pageNum: this.pageNum,
              pageSize: this.pageSize,
              categoryId:this.filterForm.categoryId,
              title:this.filterForm.title,
              ...(this.filterForm.dateRange && this.filterForm.dateRange.length === 2 && {
                beginDate: this.filterForm.dateRange[0],
                endDate: this.filterForm.dateRange[1]
              })
            }
          }
      ).then(response =>{
        this.tableData = response.data.data.list
        this.total = response.data.data.total
      })
    },
    // 分页大小变化
    handleSizeChange (val){
      this.pageSize = val;
      this.pageNum = 1;
      this.showData();
    },
    // 当前页变化
    handleCurrentChange(val){
      this.pageNum = val
      this.showData();
    },
    // 搜索处理
     handleSearch (){
      // console.log('搜索条件：', filterForm);
      // 这里实际项目中应该调用API获取数据
       this.showData();
    },
    // 重置表单
    resetForm(){
      this.filterForm.title = '';
      this.filterForm.categoryId = '';
      this.filterForm.dateRange = [];
    },
    // 删除新闻
    handleDelete(row){
      this.$confirm(`确定要删除该新闻吗?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() =>{
        axios.post("http://localhost:8080/news/delete/"+row.id).then((resp) => {
          if(resp.data.code == 200){
            this.$message({
              message:"恭喜你，这是一条成功消息",
              type:"success"
            })
            this.handleCurrentChange(this.pageNum)
          }else{
            this.$message.error(resp.data.msg)
          }
        })
      })
    },
    // 查看详情
    handleView(row) {
      console.log('查看：', row);
    },
  },
  created() {
    this.showData();
    this.category();
  }

}
</script>



<style scoped>
.filter-container {
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>